package com.jrh.biz.product.ref;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jrh.common.constant.JrhBizConstant;
import com.jrh.dal.ProductRefDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.Product;
import com.jrh.domain.entity.ProductRef;

public class ProductRefServiceImpl implements ProductRefService {

    private ProductRefDao productRefDao;

    public void setProductRefDao(ProductRefDao productRefDao) {
        this.productRefDao = productRefDao;
    }

    @Override
    public Page<ProductRef> getProductAdPage(Long brandId, Integer start, Integer pageSize) {
        Page<ProductRef> prList = productRefDao.getProductAdPage(brandId, JrhBizConstant.PRODUCT_REF_TYPE_AD,
                                                                 (start - 1) * pageSize, pageSize);
        List<ProductRef> prefList = new ArrayList<ProductRef>();
        ProductRef lastPr = null;
        for (ProductRef pr : prList.getDatas()) {
            Product p = new Product();
            p.setImgUrl(pr.getProductImgUrl());
            p.setId(pr.getProductId());
            p.setName(pr.getProductName());
            if (lastPr == null) {
                prefList.add(pr);
                pr.addProduct(p);
            } else if (!pr.getId().equals(lastPr.getId())) {
                prefList.add(pr);
                if (p.getId() != null) {
                    pr.addProduct(p);
                }
            } else if (pr.getId().equals(lastPr.getId())) {
                prefList.get(prefList.size() - 1).addProduct(p);
            }
            lastPr = pr;
        }
        prList.setDatas(prefList);
        return prList;
    }

    @Override
    public Page<ProductRef> getProductGroupPage(Long brandId, Integer start, Integer pageSize) {

        Page<ProductRef> prList = productRefDao.getProductGroupPage(brandId, JrhBizConstant.PRODUCT_REF_TYPE_GROUP,
                                                                    (start - 1) * pageSize, pageSize);
        List<ProductRef> prefList = new ArrayList<ProductRef>();
        ProductRef lastPr = null;
        for (ProductRef pr : prList.getDatas()) {
            Product p = new Product();
            p.setImgUrl(pr.getProductImgUrl());
            p.setId(pr.getProductId());
            p.setName(pr.getProductName());
            if (lastPr == null) {
                prefList.add(pr);
                pr.addProduct(p);
            } else if (!pr.getId().equals(lastPr.getId())) {
                prefList.add(pr);
                if (p.getId() != null) {
                    pr.addProduct(p);
                }
            } else if (pr.getId().equals(lastPr.getId())) {
                prefList.get(prefList.size() - 1).addProduct(p);
            }
            lastPr = pr;
        }
        prList.setDatas(prefList);
        return prList;
    }

    @Override
    public void deleteProductAdById(Long adId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("refType", JrhBizConstant.PRODUCT_REF_TYPE_AD);
        map.put("refId", adId);
        productRefDao.deleteProductAd(adId);
        productRefDao.deleteProductRef(map);
    }

    @Override
    public void deleteProductGroupById(Long groupId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("refType", JrhBizConstant.PRODUCT_REF_TYPE_GROUP);
        map.put("refId", groupId);
        productRefDao.deleteProductGroup(groupId);
        productRefDao.deleteProductRef(map);
    }

    @Override
    public List<ProductRef> listSameGroupProduct(Long productId) {
        List<ProductRef> prList = productRefDao.listSameTypeProduct(productId, JrhBizConstant.PRODUCT_REF_TYPE_GROUP,
                                                                    PRODUCT_GROUP_PAGE_SIZE);
        Map<Long, ProductRef> prMap = new HashMap<Long, ProductRef>();
        List<ProductRef> newPrList = new ArrayList<ProductRef>();
        for (ProductRef pr : prList) {
            if (pr == null) {
                continue;
            }
            if (!productId.equals(pr.getProductId()) && !prMap.containsKey(pr.getProductId())) {
                newPrList.add(pr);
            }
            prMap.put(pr.getProductId(), pr);
        }
        return newPrList;
    }

    @Override
    public ProductRef getProductAd(Long productId) {
        return productRefDao.getProductAd(productId, JrhBizConstant.PRODUCT_REF_TYPE_AD);
    }

    @Override
    public void updateProductRef(Long refId, String refType, String[] productIds) {
        if (productIds == null || productIds.length == 0 || refId == null || StringUtils.isEmpty(refType)) {
            return;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("refId", refId);
        map.put("refType", refType);
        productRefDao.deleteProductRef(map);
        for (String productId : productIds) {
            ProductRef ref = new ProductRef();
            try {
                ref.setProductId(Long.parseLong(productId));
                ref.setRefId(refId);
                ref.setRefType(refType);
                productRefDao.create(ref);
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void createProductAd(String name, String imageUrl, Long brandId) {
        ProductRef productRef = new ProductRef();
        productRef.setName(name);
        productRef.setImageUrl(imageUrl);
        productRef.setBrandId(brandId);
        productRefDao.createProductAd(productRef);
    }

    @Override
    public void createProductGroup(String name, String desc, Long brandId) {
        ProductRef productRef = new ProductRef();
        productRef.setName(name);
        productRef.setDescn(desc);
        productRef.setBrandId(brandId);
        productRefDao.createProductGroup(productRef);
    }
}
